package com.tabuleiro.negocio.base;

import java.io.Serializable;
import java.util.Map;

import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.hibernate.Session;

public interface DefaulService <T, K extends Serializable> {
	
	 /**
     * Creates a new object for the given type. After a call to this method the entity will be
     * persisted into database and then refreshed. Also current persistent Session will be flushed.
     * 
     * @param <T>
     * @param t
     * @return persisted Object
     */
    @CommitAfter
    <T> T create(T t);

    /**
     * Updates the given object
     * 
     * @param <T>
     * @param t
     * @return persisted object
     */
    @CommitAfter
    <T> T update(T t);

    /**
     * Deletes the given object by id
     * 
     * @param <T>
     * @param <PK>
     * @param type
     *            , entity class type
     * @param id
     */
    @CommitAfter
    <T, PK extends Serializable> void delete(Class<T> type, PK id);

    /**
     * Finds an object by id
     * 
     * @param <T>
     * @param <PK>
     * @param type
     * @param id
     * @return the object
     */
    <T, PK extends Serializable> T find(Class<T> type, PK id);
	
	/**
	 * Invokes <code>entityManager().refresh()<code>.
	 * @param object
	 */
	void refresh(T object);

	/**
	 * Attaches an object to the persistence context, if there is one. The object store
	 * must not be changed. The attached object, which not necessarily is the one passed
	 * as a parameter, is returned.
	 * 
	 * @param object a <code>T</code>.
	 * @return object a <code>T</code>.
	 */
	T attach(T object);
	
	/**
	 * Removes an object from the persistence context, the object status will be
	 * changed to transient
	 * @param object a <code>T</code>.
	 */
	void discard(T object);
	
	T findUniqueWithNamedQuery(String query,  Map<String, Object> parametros);
	
	Session getSession();

}
